﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{

public class Identity {

// 位权值数组
private static byte[] Wi=new byte[17];
// 身份证前部分字符数
private static  byte fPart = 6;
// 身份证算法求模关键值
private static  byte fMod = 11;
// 旧身份证长度
private static  byte oldIDLen = 15;
// 新身份证长度
private static  byte newIDLen = 18;
// 新身份证年份标志
private static  String yearFlag = "19";

// 校验码串
private static  String CheckCode="10X98765432";
// 最小的行政区划码
private static  int minCode = 150000;
// 最大的行政区划码
private static  int maxCode = 700000;

// 旧身份证号码
// private String oldIDCard="";
// 新身份证号码
// private String newIDCard="";
// 地区及编码


//private String Area[][2] =
private static void setWiBuffer(){
for(int i=0;i < Wi.Length ; i ++)
{
int k = (int) Math.pow(2, (Wi.length-i));
Wi[i] = (byte)(k % fMod);
}
}


//获取新身份证的最后一位:检验位
private static String getCheckFlag(String idCard){
int sum = 0;
//进行加权求和
for(int i=0; i<17; i++){
sum += int.Parse(idCard.Substring(i,i+1)) * Wi[i];
}

//取模运算，得到模值
byte iCode = (byte) (sum % fMod);
return CheckCode.Substring(iCode,iCode+1);
}

//判断串长度的合法性
private static bool checkLength( String idCard,bool newIDFlag){
bool right = (idCard.length() == oldIDLen) || (idCard.length() == newIDLen);
newIDFlag = false;
if(right){
newIDFlag = (idCard.length() == newIDLen);
}
return right;
}

//获取时间串
private static String getIDDate( String idCard,bool newIDFlag){
String dateStr = "";
if(newIDFlag)
dateStr = idCard.Substring(fPart,fPart+8);
else
dateStr = yearFlag + idCard.Substring(fPart,fPart+6);
return dateStr;
}

//判断时间合法性
private static bool checkDate( String dateSource){
String dateStr = dateSource.Substring(0,4)+"-"+dateSource.Substring(4,6)+"-"+dateSource.Substring(6,8);
System.out.println(dateStr);
DateFormat df = DateFormat.getDateInstance();
df.setLenient(false);
try {
Date date= df.parse(dateStr);
return (date!=null);
} catch (ParseException e) {
// TODO Auto-generated catch block
return false;
}
}

//旧身份证转换成新身份证号码
public static String getNewIDCard( String oldIDCard){
//初始化方法
Identity.setWiBuffer();
if(!checkIDCard(oldIDCard)){
return oldIDCard;
}
String newIDCard = oldIDCard.Substring(0, fPart);
newIDCard += yearFlag;
newIDCard += oldIDCard.Substring(fPart, oldIDCard.length());
String ch = getCheckFlag(newIDCard);
newIDCard += ch;
return newIDCard;
}

//新身份证转换成旧身份证号码
public static String getOldIDCard( String newIDCard){
//初始化方法
Identity.setWiBuffer();
if(!checkIDCard(newIDCard)){
return newIDCard;
}
String oldIDCard = newIDCard.Substring(0,fPart)+
newIDCard.Substring(fPart+yearFlag.length(),newIDCard.length()-1);
return oldIDCard;
}

//判断身份证号码的合法性
public static bool checkIDCard( String idCard){
//初始化方法
Identity.setWiBuffer();
bool isNew = false //String message = "";
if (!checkLength(idCard,isNew)){
//message = "ID长度异常";
return false;
}
String idDate = getIDDate(idCard, isNew);
if(!checkDate(idDate)){
//message = "ID时间异常";
return false;
}
if(isNew){
String checkFlag = getCheckFlag(idCard);
String theFlag = idCard.Substring(idCard.length()-1,idCard.length());
if(!checkFlag.equals(theFlag)){
//message = "新身份证校验位异常";
return false;
}
}
return true;
}

//获取一个随机的"伪"身份证号码
public static String getRandomIDCard( bool idNewID){
//初始化方法
Identity.setWiBuffer();
Random ran = new Random();
String idCard = getAddressCode(ran)+getRandomDate(ran,idNewID)+getIDOrder(ran);
if(idNewID){
String ch = getCheckFlag(idCard);
idCard += ch;
}
return idCard;
}

//产生随机的地区编码
private static String getAddressCode(Random ran) {
if(ran==null){
return "";
}else{
int addrCode = minCode + ran.nextInt(maxCode-minCode);
return Integer.toString(addrCode);
}
}

//产生随机的出生日期
private static String getRandomDate(Random ran, bool idNewID) {
// TODO Auto-generated method stub
if(ran==null){
return "";
}
int year = 0;
if(idNewID){
year = 1900 + ran.nextInt(2007-1900);
}else{
year = 1 + ran.nextInt(99);
}
int month = 1+ran.nextInt(12);
int day = 0;
if(month==2){
day= 1+ran.nextInt(28);
}else if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){
day= 1+ran.nextInt(31);
}else{
day= 1+ran.nextInt(30);
}
NumberFormat nf = NumberFormat.getIntegerInstance();
nf.setMaximumIntegerDigits(2);
nf.setMinimumIntegerDigits(2);
String dateStr = Integer.toString(year)+nf.format(month)+nf.format(day);
return dateStr;
}

//产生随机的序列号
private static String getIDOrder(Random ran) {
// TODO Auto-generated method stub
NumberFormat nf = NumberFormat.getIntegerInstance();
nf.setMaximumIntegerDigits(3);
nf.setMinimumIntegerDigits(3);
if(ran==null){
return "";
}else{
int order = 1+ran.nextInt(999);
return nf.format(order);
}
}

public Identity(){
setWiBuffer();
}


}
